package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSON;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.sequence.service.facade.SequenceService;
import com.fdb.efp.nls.common.LimitStatusEnum;
import com.fdb.efp.nls.service.dao.CustomerLimitInfoDao;
import com.fdb.efp.nls.service.domain.CustomerLimitInfo;
import com.fdb.efp.nls.service.facade.CustomerLimitInfoService;
import com.fdb.efp.nls.service.vo.CustomerLimitInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 
  * TODO ServiceImpl: 客户限额	
  * @author wanglihua
  * @since 2019年3月06日 下午3:30:01
  * @version 1.0
 */
@Service("customerLimitInfoService")
public class CustomerLimitInfoServiceImpl implements CustomerLimitInfoService, FrameworkService{
	
	private static Logger logger = LoggerFactory.getLogger(CustomerLimitInfoServiceImpl.class);
	
	@Autowired
	private CustomerLimitInfoDao customerLimitInfoDao;
	
	@Autowired
	private SequenceService sequenceService;
	/**
	 * 新增操作
	 */
	@Override
	public int insertCustomerLimitInfo(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前新增数据为:"+ customerLimitInfoVO.toString());
		int num = 0;
		try {
			customerLimitInfoVO.setSerno(sequenceService.getSequence("LIMIT_NO", null, null));
			CustomerLimitInfo customerLimitInfo = new CustomerLimitInfo();
			beanCopy(customerLimitInfoVO, customerLimitInfo);
			//检查是否已存在同客户类型，同限额类型的有效额度
			boolean isDup = checkData(customerLimitInfo);
			if (isDup) return num = -2;
			//通过检查后进行新增操作
			customerLimitInfo.setCreateTime(TimeUtil.getCurrentDateTime());
			num = customerLimitInfoDao.insertCustomerLimitInfo(customerLimitInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前删除数据条件为:"+ customerLimitInfoVO);
		int num = 0;
		try {
			CustomerLimitInfo customerLimitInfo = new CustomerLimitInfo();
			beanCopy(customerLimitInfoVO, customerLimitInfo);
			num = customerLimitInfoDao.deleteByPk(customerLimitInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ customerLimitInfoVO +"删除的数据条数为"+ num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前修改数据为:"+ customerLimitInfoVO.toString());
		int num = 0;
		try {
			CustomerLimitInfo customerLimitInfo = new CustomerLimitInfo();
			beanCopy(customerLimitInfoVO, customerLimitInfo);
			customerLimitInfo.setLastChgTime(TimeUtil.getCurrentDateTime());
			num = customerLimitInfoDao.updateByPk(customerLimitInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ customerLimitInfoVO +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public int updateQuotaTypeByPk(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前修改数据为:" + customerLimitInfoVO.toString());
        int num = 0;
        try {
    		CustomerLimitInfo customerLimitInfo = new CustomerLimitInfo();
            beanCopy(customerLimitInfoVO, customerLimitInfo);
			//检查是否已存在同客户类型，同限额类型的有效额度
			boolean isDup = checkData(customerLimitInfo);
			if (isDup) return num = -2;
            //通过检查后进行修改操作
            setUpdateDefaultProperty(customerLimitInfo);
            customerLimitInfo.setLastChgTime(TimeUtil.getCurrentDateTime());
            num = customerLimitInfoDao.updateQuotaTypeByPk(customerLimitInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
        logger.debug("根据条件:"+ customerLimitInfoVO +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public CustomerLimitInfoVO queryByPk(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前查询参数信息为:"+ customerLimitInfoVO);
		try {
			CustomerLimitInfo queryBdMoneyDrawTemp = new CustomerLimitInfo();
			beanCopy(customerLimitInfoVO, queryBdMoneyDrawTemp);
			CustomerLimitInfo queryRslBdMoneyDrawTemp = customerLimitInfoDao.queryByPk(queryBdMoneyDrawTemp);
			if (Objects.nonNull(queryRslBdMoneyDrawTemp)) {
				CustomerLimitInfoVO outBdMoneyDrawTempVo = beanCopy(queryRslBdMoneyDrawTemp, new CustomerLimitInfoVO());
				logger.debug("当前查询结果为:"+ outBdMoneyDrawTempVo.toString());
				return outBdMoneyDrawTempVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerLimitInfoVO> queryAllByPage(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前查询参数信息为:");
		CustomerLimitInfo customerLimitInfo = beanCopy(customerLimitInfoVO, new CustomerLimitInfo());
		List<CustomerLimitInfo> customerLimitInfos = customerLimitInfoDao.queryAllByPage(customerLimitInfo);
		logger.debug("当前查询结果集数量为:" + customerLimitInfos.size());
		List<CustomerLimitInfoVO> list = null;
		try {
			pageSet(customerLimitInfos, customerLimitInfo);
			list = (List<CustomerLimitInfoVO>) beansCopy(customerLimitInfos, CustomerLimitInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public CustomerLimitInfoVO queryByStsAndCusTyp(CustomerLimitInfoVO customerLimitInfoVO) {
		logger.debug("当前查询参数信息为:"+ customerLimitInfoVO);
		CustomerLimitInfoVO result = null;
		try {
			CustomerLimitInfo customerLimitInfo = new CustomerLimitInfo();
			beanCopy(customerLimitInfoVO, customerLimitInfo);
			CustomerLimitInfo queryRslBdMoneyDrawTemp = customerLimitInfoDao.queryByStsAndCusTyp(customerLimitInfo);
			if (Objects.nonNull(queryRslBdMoneyDrawTemp)) {
				result = beanCopy(queryRslBdMoneyDrawTemp, new CustomerLimitInfoVO());
				logger.debug("当前查询结果为:"+ JSON.toJSONString(result));
				return result;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return result;
	}

	/**
	 *
	 * 检查是否已存在同客户类型，同限额类型的有效额度
	 *
	 * @author wanglihua
	 * @params  * @param null
	 * @return
	 * @since 2019/8/27 14:28
	 * @date 2019/8/27 14:28
	 * @version 0.1
	 */
	private boolean checkData(CustomerLimitInfo customerLimitInfo) {
		if (LimitStatusEnum.QUOTA_LIMIT_STATUS_01.getKey().equals(customerLimitInfo.getSts())) {
			List<CustomerLimitInfo> queryRs = customerLimitInfoDao.queryByCondition(customerLimitInfo);
			if (!Objects.isNull(queryRs) && !queryRs.isEmpty()){
				return true;
			}
		}
		return false;
	}

	@Override
	public int inValidQuotaLimitOverEndDt() {
		LocalDate date = LocalDate.now();
		logger.info("======>失效额度限制日期到期的信息开始，当前日期:<======"+date);
		int num = 0;
		try {
			num = customerLimitInfoDao.inValidQuotaLimitOverEndDt();
			logger.info("======>失效额度限制日期到期的信息结束，失效的额度限制条数为:<======"+num);
		} catch (Exception e) {
			num = -1;
			logger.error("失效额度限制日期到期的信息发生异常，异常信息为:"+e.getMessage());
		}
		return num;
	}
}
